<?php
/**
 * Created by lzl
 * Date: 2021 2021/11/9
 * Time: 15:11
 */
namespace App\Http\Controllers\Admin;

use App\Forms\OrderForms;
use App\Forms\ExpressForms;
use App\Forms\PartnerForms;
use App\Models\Express;
use App\Services\Admin\OrderServices;

class OrderController extends Controller
{

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function orderList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
                'order_sn'   => 'nullable',
                'status'     => 'nullable'
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new OrderForms())->getOrderList($this->validated));
        }

        return $this->view('order.order_list');
    }


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function partnerList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
                'order_sn'   => 'nullable',
                'status'     => 'nullable'
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new PartnerForms())->getOrderList($this->validated));
        }

        return $this->view('order.partner_order');
    }


    /**
     * 订单详情
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \App\Exceptions\RequestException
     */
    public function orderDetail()
    {
        $rules = [
            'id' => 'required'
        ];

        $this->validateInput($rules);

        $order = (new OrderForms())->getOrderDetail($this->validated['id']);

        return $this->view('order.detail', compact('order'));
    }


    /**
     * 订单发货
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     * @throws \Throwable
     */
    public function deliver()
    {
        $rules = [
            'id'         => 'required',
            'express_id' => 'nullable',
            'express_sn' => 'nullable',
        ];

        $this->validateInput($rules);

        if($this->request->isMethod('post')) {
            return $this->success((new OrderServices())->deliver($this->validated));
        }

        $expressForms = new ExpressForms();

        $express = $expressForms->getExpressList(['status' => Express::USED ]);

        $order_express = $expressForms->getOrderExpress($this->validated['id']);

        return $this->view('order.deliver', array_merge(compact('express','order_express'), ['id' => $this->validated['id']]));
    }


    /**
     * 更新物流信息
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function updateExpress()
    {
        $rules = [
            'id' => 'required',
        ];

        $this->validateInput($rules);

        return $this->success((new ExpressForms())->updateOrderExpress($this->validated['id']));
    }

}